/**
 * 获取form内的表单元素的值
 * @param form
 * @return { name1: value1, name2: value2}
 * @example const res=get_data(from);
 */

export default function get_data(form: HTMLFormElement) {
	const eles = form.elements;
	return (Array.from(eles) as HTMLInputElement[]).filter((i) => {
		return i.type !== '' && i.name !== '' && i.value !== undefined;
	}).reduce((p, c) => {
		const key = c.name;
		const value = (() => {
			const t = c.type;
			switch (t) {
				case 'number':
					return parseFloat(c.value);
				case 'checkbox':
					return c.checked;
				case 'radio':
					return c.checked;
				default:
					return c.value;
			}
		})();
		p[key] = value;
		return p;
	}, {} as { [name: string]: string | number | boolean });
}
